package com.icare.shop.server.service;

import com.icare.common.service.utils.RedissonUtils;
import com.icare.shop.server.config.MyClassLoader;
import lombok.extern.slf4j.Slf4j;
import org.redisson.RedissonBloomFilter;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import sun.misc.IOUtils;
import sun.nio.ch.ThreadPool;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.concurrent.ExecutorService;

/**
 * <pre>
 *    author  : tianhaibo
 *    email   : tianhaibo@jshuii.com
 *    time    : 2020/5/11   17:04
 *    desc    :
 *    version : v1.0
 * </pre>
 */
@Slf4j
@Service
public class DebugService {

    public Object load(String className,String methodName,byte[] bytes){
        try {
            File file = new File("bracelet-shop/shop-server/src/main/resources/statics/ThreadPoolConfig.class");
            System.out.println(file.getAbsolutePath());
            Class<?> aClass = MyClassLoader.loadClass(className, bytes);
            if(aClass!=null){

                Object o = aClass.newInstance();
                if(o!=null){
                    Method method = aClass.getDeclaredMethod(methodName);
                    method.setAccessible(true);
                    Object invoke = method.invoke(o);
                    return invoke;
                }
            }
        } catch (Exception e) {
            log.error("",e);
        }
        return null;
    }

    @Async
    public void lock(String key, Integer time) {
        try {
            RedissonUtils.lock(key,time);
            Thread.sleep(time*1000L);
        }catch (Exception e){
            log.error("加锁异常",e);
        }finally {
            RedissonUtils.unlock(key);
        }
        return;
    }
}
